﻿using Volo.Abp.Modularity;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using MyAbpFrame.Application.User;
using MyAbpFrame.Application;
using Volo.Abp.Autofac;
using MyAbpFrame.Application.Contracts;
using MyAbpFrame.EntityFrameworkCore;
using Microsoft.OpenApi.Models;

namespace MyAbpFrame.WebMvc
{
    [DependsOn(
        typeof(AbpAspNetCoreMvcModule),
        typeof(AbpAutofacModule),
        typeof(AppApplicationContractsModule),
       /* typeof(AppEntityFrameworkCoreModule),*/ //放到application层
        typeof(AppApplicationModule) // 区别services.AddSingleton<IUserAppService, UserAppService>(); 此为方式一,相应类需继承的IApplicationService(ApplicationService)接口
        )]
    public class AppModule:AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var services = context.Services;
            //services.AddSingleton<IUserAppService, UserAppService>(); //方式二,可以去掉继承的IApplicationService(ApplicationService)接口

            services.AddControllersWithViews();

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "MyAbpFrame API", Version = "v1" });
                options.DocInclusionPredicate((docName, description) => true);
                options.CustomSchemaIds(type => type.FullName);
            });

            Configure<AbpAspNetCoreMvcOptions>(opt =>
            {
                opt.ConventionalControllers.Create(typeof(AppApplicationModule).Assembly);
            });
        }
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Login}/{action=Index}/{id?}");
            });

            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "MyAbpFrame API V1");
                //options.IndexStream = () => Assembly.GetExecutingAssembly()
                //   .GetManifestResourceStream("dc.Haiyakj.Web.Host.wwwroot.swagger.ui.index.html");
            }); //URL: /swagger
        }
    }
}
